using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using XFGameFramework;
public class FightPanel : Panel
{
    private SkillController skillController => Module.LoadController<SkillController>();
    private GameController gameController => Module.LoadController<GameController>();
    private PlayerController playerController => Module.LoadController<PlayerController>();
    private EnemyController enemyController => Module.LoadController<EnemyController>();
    #region 字段
    [SerializeField] private Text text_enemy_name;
    [SerializeField] private Text text_enemy_level;
    [SerializeField] private Scrollbar slider_enemy_HP;
    [SerializeField] private Text text_player_name;
    [SerializeField] private Text text_player_level;
    [SerializeField] private Scrollbar slider_player_HP;
    [SerializeField] private Text text_introduction;
    [SerializeField] private Button btn_confirm;
    [SerializeField] private Button btn_concel;
    [SerializeField] private Button[] btn_skill;
    [SerializeField] private GameObject Fight;

    [SerializeField] private Text text_enemy_HP;
    [SerializeField] private Text text_player_HP;

    [SerializeField] private Text text_Win_Content;
    [SerializeField] private Text text_Lose_Content;

    [SerializeField] private Button btn_win_out;
    [SerializeField] private Button btn_lose_out;

    [SerializeField] private Image image_win_bg;
    [SerializeField] private Image image_lose_bg;
    [SerializeField] private Panel Fightpanel;

    [SerializeField] private Text skill_name_1;
    [SerializeField] private Text skill_name_2;
    [SerializeField] private Text skill_name_3;
    [SerializeField] private Text skill_name_4;
    [SerializeField] private Image skill_cooling_1;
    [SerializeField] private Image skill_cooling_2;
    [SerializeField] private Image skill_cooling_3;
    [SerializeField] private Image skill_cooling_4;


    [SerializeField] private Text skill_cooling_text_1;
    [SerializeField] private Text skill_cooling_text_2;
    [SerializeField] private Text skill_cooling_text_3;
    [SerializeField] private Text skill_cooling_text_4;


    private void Reset()
    {

        text_enemy_name = transform.Find("EnemyContent/bg/text_enemy_name").GetComponent<Text>();
        text_enemy_level = transform.Find("EnemyContent/bg/text_enemy_level").GetComponent<Text>();
        slider_enemy_HP = transform.Find("EnemyContent/bg/slider_enemy_HP").GetComponent<Scrollbar>();
        text_player_name = transform.Find("PlayerContent/bg/text_player_name").GetComponent<Text>();
        text_player_level = transform.Find("PlayerContent/bg/text_player_level").GetComponent<Text>();
        slider_player_HP = transform.Find("PlayerContent/bg/slider_player_HP").GetComponent<Scrollbar>();
        text_introduction = transform.Find("bg_text/start_text/text_introduction").GetComponent<Text>();
        btn_confirm = transform.Find("bg_text/start_text/btn_confirm").GetComponent<Button>();
        btn_concel = transform.Find("bg_text/start_text/btn_concel").GetComponent<Button>();
        text_enemy_HP = transform.Find("EnemyContent/bg/slider_enemy_HP/EnemyHpText").GetComponent<Text>();
        text_player_HP = transform.Find("PlayerContent/bg/slider_player_HP/PlayerHpText").GetComponent<Text>();

    }

    #endregion    
    
    public PlayerModel player;
    private int player_FullHP;
    private int enemy_FullHP;
    #region 方法
    public override void OnLoaded(params object[] param)
    {
        base.OnLoaded(param);
        Debug.Log("Loaded FightPanel");
        player = playerController.GetPlayerModel();
        
        player_FullHP = player.maxHp;
        enemy_FullHP = enemyController.fullHp;
    }

    public void OnBtnOutClick()
    {
        gameController.SetGameState(GameState.Game_Ready);
        Close();
    }
    public void OnBtnConfirmClick()
    {
        GameObject Start_text = GameObject.Find("bg_text/start_text");
        Start_text.SetActive(false);

        Fight.SetActive(true);
        gameController.SetGameState(GameState.Fight_Player);
    }

    public void onBtnAttackClick(int index)//释放技能按钮
    {
        int ID = playerController.GetSkillList()[index];
        skillController.ReleaseSkill(ID, player, enemyController.GetEnemyModel());

        gameController.SetGameState(GameState.Fight_Enemy);
    }


    public void OnBtnConcelClick()
    {
        //TODO
    }
    #endregion
    #region UI更新
    public void UpdatePlayerName(string playerName = "Player")
    {
        text_player_name.text = playerName;
    }

    // 更新玩家等级
    public void UpdatePlayerLevel(int playerLevel = 666)
    {
        text_player_level.text = "Lv. " + playerLevel.ToString();
    }
    // 更新玩家血量
    public void UpdatePlayerHpText(int playerHp, int fullHp)
    {
        text_player_HP.text = playerHp.ToString() + "/" + fullHp.ToString();
        slider_player_HP.size = (float)playerHp / (float)fullHp;
        player_FullHP = fullHp;

    }

    // 更新敌人名字
    public void UpdateEnemyName(string enemyName = "Enemy")
    {
        text_enemy_name.text = enemyName;
    }

    // 更新敌人等级
    public void UpdateEnemyLevel(int enemyLevel = 111)
    {
        text_enemy_level.text = "Lv. " + enemyLevel.ToString();
    }

    // 更新敌人血量
    public void UpdateEnemyHpText(int enemyHp = 333, int fullHp = 555)
    {
        text_enemy_HP.text = enemyHp.ToString() + "/" + fullHp.ToString();
        slider_enemy_HP.size = (float)enemyHp / (float)fullHp;
        enemy_FullHP = fullHp;
    }
    //仅仅更新敌人血量
    public void UpdateEnemyHp(int hp)
    {
        text_enemy_HP.text = hp.ToString() + "/" + enemy_FullHP.ToString();
        slider_enemy_HP.size = (float)hp / (float)enemy_FullHP;

    }
    //仅仅显示玩家血量
    public void UpdatePlayerHp(int hp)
    {
        text_player_HP.text = hp.ToString() + "/" + player_FullHP.ToString();
        slider_player_HP.size = (float)hp / (float)player_FullHP;
    }

    // 更新所有信息
    public void UpdateAllText(PlayerModel player, EnemyModel enemy)
    {
        if (player == null)
        {
            Debug.LogError("PlayerModel is null");
            return;
        }
        if (enemy == null)
        {
            Debug.LogError("EnemyModel is null");
            return;
        }
        //Debug.LogFormat("UpdateAllText player:{0}", player.name);
        UpdatePlayerName(playerController.name);
        UpdatePlayerLevel(playerController.level);
        UpdatePlayerHpText(playerController.CurrentHp, playerController.MaxHp);

        UpdateEnemyName(enemyController.name);
        UpdateEnemyLevel(enemyController.level);
        //Debug.Log("enemy.currentHp:" + enemy.currentHp);
        UpdateEnemyHpText(enemy.currentHp, enemy.maxHp);
        Updateskillcd();
        UpdateSkillsBtn();
    }

    public void Updateskillcd()
    {
        List<int> skillCd = playerController.GetSkillCd();
        for (int i = 0; i < skillCd.Count; i++)
        {
            if (skillCd[i] > 0)
            {
                switch (i)
                {
                    case 0:
                        btn_skill[0].interactable = false;
                        skill_cooling_1.gameObject.SetActive(true);
                        skill_cooling_text_1.text = "冷却中 " + skillCd[0].ToString() + "回合";
                        break;
                    case 1:
                        btn_skill[1].interactable = false;
                        skill_cooling_2.gameObject.SetActive(true);
                        skill_cooling_text_2.text = "冷却中 " + skillCd[1].ToString() + "回合";
                        break;
                    case 2:
                        btn_skill[2].interactable = false;
                        skill_cooling_3.gameObject.SetActive(true);
                        skill_cooling_text_3.text = "冷却中 " + skillCd[2].ToString() + "回合";
                        break;
                    case 3:
                        btn_skill[3].interactable = false;
                        skill_cooling_4.gameObject.SetActive(true);
                        skill_cooling_text_4.text = "冷却中 " + skillCd[3].ToString() + "回合";
                        break;
                }
            }
            else if (skillCd[i] == 0)
            {
                switch (i)
                {
                    case 0:
                        btn_skill[0].interactable = true;
                        skill_cooling_1.gameObject.SetActive(false);
                        break;
                    case 1:
                        btn_skill[1].interactable = true;
                        skill_cooling_2.gameObject.SetActive(false);
                        break;
                    case 2:
                        btn_skill[2].interactable = true;
                        skill_cooling_3.gameObject.SetActive(false);
                        break;
                    case 3:
                        btn_skill[3].interactable = true;
                        skill_cooling_4.gameObject.SetActive(false);
                        break;
                }
            }
        }
    }
    //每回合更新的信息（仅仅血量与BUFF，技能CD）
    public void UpdateHPText(PlayerModel player, PlayerModel enemy)
    {
        UpdatePlayerHp(playerController.CurrentHp);
        UpdateEnemyHp(enemyController.CurrentHp);
        Updateskillcd();
        UpdateSkillsBtn();
    }
    public void UpdateSkillsBtn()
    {
        for (int i = 0; i < btn_skill.Length; i++)
        {
            int index = i;
            Button button = btn_skill[index];
            button.onClick.RemoveAllListeners();
            btn_skill[index].onClick.AddListener(() =>
            {
                onBtnAttackClick(index);
            });
        }
    }
    #endregion
}

